package com.example.imgrecognition;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.widget.Button;

import com.beardedhen.androidbootstrap.AwesomeTextView;
import com.example.imgrecognition.Activity.ImagecontrastActivity;
import com.example.imgrecognition.Activity.TakePhotoActivity;
import com.example.imgrecognition.Activity.TakeVideoActivity;
import com.example.imgrecognition.Activity.TestRXActivity;
import com.example.imgrecognition.Util.ToastUtil;
import com.example.imgrecognition.service.StateService;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TestNetActivity extends AppCompatActivity {

    @BindView(R.id.conn_btn)
    Button connBtn;
    @BindView(R.id.takephoto_btn)
    Button takephoto_btn;
    @BindView(R.id.takevideo_btn)
    Button takevideo_btn;
    @BindView(R.id.imagecon_btn)
    Button imagecon_btn;
    @BindView(R.id.mtext)
    AwesomeTextView mtext;
    @BindView(R.id.rx_btn)
    Button rxBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_net);
        ButterKnife.bind(this);
        if (isWiFiActive(TestNetActivity.this)) {
            ToastUtil.show(TestNetActivity.this, "已连接网络");
        } else {
            ToastUtil.show(TestNetActivity.this, "已断开网络");
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent();
        intent.setClass(TestNetActivity.this, StateService.class);
        startService(intent);
    }

    @OnClick(R.id.conn_btn)
    public void onViewClicked() {
        String imei = "";
        Context context = getApplicationContext();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            imei = telephonyManager.getDeviceId();
        }
        mtext.setText(imei + "00" + "    请将此号码添加到后台");
       /* String msg = "901651507";
        UdpClient.sendMessage(msg.getBytes());*/
    }

    @OnClick(R.id.takephoto_btn)
    public void onClicked() {
        startActivity(new Intent(TestNetActivity.this, TakePhotoActivity.class));
    }

    @OnClick(R.id.takevideo_btn)
    public void onVideobtn() {
        startActivity(new Intent(TestNetActivity.this, TakeVideoActivity.class));
    }

    @OnClick(R.id.imagecon_btn)
    public void imagecon_btn() {
        startActivity(new Intent(TestNetActivity.this, MainActivity.class));
    }

    public static boolean isWiFiActive(Context inContext) {

        WifiManager mWifiManager = (WifiManager) inContext.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
        int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
        if (mWifiManager.isWifiEnabled() && ipAddress != 0) {
            System.out.println("**** WIFI is on");
            return true;
        } else {
            System.out.println("**** WIFI is off");
            return false;
        }
    }


    @OnClick(R.id.rx_btn)
    public void onRxClicked() {
        Intent intent = new Intent(TestNetActivity.this, TestRXActivity.class);
        startActivity(intent);
    }
}
